package com.sl.au.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ContractInfo;
import com.sl.au.entity.ContractInfoQuoteDevice;
import com.sl.au.entity.Module;
import com.sl.au.service.ContractInfoQuoteDeviceService;
import com.sl.au.service.ExcelService;

@RestController
@RequestMapping(value = "/ui/contractInfoQuoteDeviceIndex")
public class ContractInfoQuoteDeviceController extends BaseController {
	
	@Autowired
	ContractInfoQuoteDeviceService contractInfoQuoteDeviceService;
	@Autowired
	ExcelService excelService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("contract/contractInfoQuoteDevice");
		return modelAndView;
	}
	/**
	 * 获取页面数据
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior();
		String areaName = a.getUser().getSaleArea().getAreaName();
		String name = "contractInfo";
		Module module = this.getPageModule(name);
		String dataFilter = module.getDataFilter();
		if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		if (userId == null) {
			userId = "";
		}
		if (superior == null) {
			superior = "";
		}
		if (areaName == null) {
			areaName = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("userId", userId);
		condition.put("superior", superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		List<ContractInfoQuoteDevice> contracts = contractInfoQuoteDeviceService.getContractInfos(start, length, condition, null, null);
		long count = contractInfoQuoteDeviceService.getContractInfosCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);	
		map.put("recordsTotal", count);
		return map;
	}
	/**导出合同
	 * @param key 组合id
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public ResponseEntity<org.springframework.core.io.Resource> excelFile()
			throws UnsupportedEncodingException {
		Map<String, String> condition = new HashMap<String, String>();
		String key = request.getParameter("key");
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior();
		String areaName = a.getUser().getSaleArea().getAreaName();
		String name = "contractInfo";
		Module module = this.getPageModule(name);
		String dataFilter = module.getDataFilter();
		if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		if (userId == null) {
			userId = "";
		}
		if (superior == null) {
			superior = "";
		}
		if (areaName == null) {
			areaName = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("userId", userId);
		condition.put("superior", superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		List<ContractInfoQuoteDevice> contracts = contractInfoQuoteDeviceService.getContractInfos(0, 100000, condition, null, null);
		return this.excelService.toExcel(contracts, "合同信息明细.xlsx");
	}
}
